<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Permission extends CI_Controller{
      protected $_templates;
      function __construct() {
          parent::__construct();
          $this->pre_message ="";
          $this->load->model('permission_model','phanquyen');
          $this->permit_lib->check_permit();
          $this->session->set_userdata(array('Url'=>uri_string()));          
      }      
      function index(){
          $data['title'] = 'Quản lý danh sách Modules';
          $data['list'] = $this->phanquyen->get_all_permission();
          $data['num'] = count($data['list']);
          $this->_templates['page'] = 'permission/list';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      
      function add(){
          $data['title'] = 'Thêm mới Modules';
          if(!empty($_POST['bt_submit'])){
             $this->form_validation->set_rules('permission_name','Tên Modules','trim|required');  
             $this->form_validation->set_rules('permission_notice','Ghi chú','');  
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
                 if($this->phanquyen->save()){
                     $this->session->set_flashdata('message','Lưu thành công');
                     redirect('permission');
                 }else{
                     $this->pre_message = 'Lưu không thành công';
                 }
             } 
          }
          $data['message'] = $this->pre_message;          
          $this->_templates['page'] = 'permission/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function edit(){
          $data['title'] = 'Cập nhật Modules';
          $data['rs'] = $this->phanquyen->get_permission_by_id();
          if(!empty($_POST['bt_submit'])){
             $this->form_validation->set_rules('permission_name','Tên Modules','trim|required');  
             $this->form_validation->set_rules('permission_notice','Ghi chú','');  
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
                 if($this->phanquyen->save()){
                     $this->session->set_flashdata('message','Lưu thành công');
                     redirect('permission');
                 }else{
                     $this->pre_message = 'Lưu không thành công';
                 }
             } 
          }
          $data['message'] = $this->pre_message;          
          $this->_templates['page'] = 'permission/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
    function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->phanquyen->delPermission($ar_id[$i]))
							$this->session->set_flashdata('message',lang('delete_success'));
						else $this->session->set_flashdata('message',lang('delete_unsuccess'));
					}
				}
			}
		}
		redirect('permission/index/0/'.$page);
	}  
  }
?>
